package org.windblog.core.filter;

import java.io.File;
import java.io.IOException;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import org.windblog.core.CategoryService;
import org.windblog.core.LinkService;
import org.windblog.core.OptionService;
import org.windblog.core.PageService;
import org.windblog.core.PostService;


public class StartupFilter implements javax.servlet.Filter{

	private ServletContext application = null;
	
	@Override
	public void destroy() {
	}

	@Override
	public void doFilter(ServletRequest req, ServletResponse res,
			FilterChain chain) throws IOException, ServletException {
		/** Define ABSPATH as this files directory */
		application.setAttribute("ABSPATH", new File("").getAbsolutePath());
		//System.out.println(application.getAttribute("ABSPATH"));
		
		/** Define CONTENT_DIR as content files directory */
		application.setAttribute("CONTENT_DIR", application.getAttribute("ABSPATH")+"\\content");
		
		/** start time variable */
		application.setAttribute("timestart", System.currentTimeMillis() );
		
		/** define DEBUG true to enable display of notices during development. */
		application.setAttribute("DEBUG", true);
				
		/** Allows for the plugins directory to be moved from the default location. */
		application.setAttribute("PLUGIN_DIR", application.getAttribute("CONTENT_DIR")+"\\plugins");
		
		/** Define INCLUDE directory */
		application.setAttribute("INCLUDE", application.getAttribute("ABSPATH")+"\\INCLUDE");

		if(OptionService.getAllOptions().size()==0)
		{
			String scheme = req.getScheme();
			String reqUrl = req.getServerName();
			int port = req.getLocalPort();
			int end = reqUrl.indexOf("/");
			String domain = "";
			if(end!=-1)
				domain = reqUrl.substring(0, end);
			else
				domain = reqUrl;
			OptionService.setOption("blogname", "OugeSite");
			OptionService.setOption("blogdescription", "Just a test blog .Powered by WindBlog !-)");
			OptionService.setOption("admin_email", "");
			OptionService.setOption("siteurl", scheme+"://"+domain+":"+port+"/");
			OptionService.setOption("stylesheet", "default");
			OptionService.setOption("charset", "UTF-8");
			OptionService.setOption("html_type", "text/html");
			OptionService.setOption("version", "0.5.0");
			OptionService.setOption("template", "default");
			OptionService.setOption("date_format", "MM dd , yyyy");
			OptionService.setOption("theme_root", "/content/themes/");
			OptionService.setOption("text_direction", "ltr");
			OptionService.setOption("language", "zh-CN");
			initItems();
		}
		
		
		
		chain.doFilter(req, res);
	}
	private void initItems() {
		CategoryService.initService();
		PageService.initService();
		PostService.initService();
		LinkService.initService();
	}

	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
		application = filterConfig.getServletContext();
	}
}
